package com.fs.adapters;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studentmanagersystem.R;
import com.example.studentmanagersystem.URL;
import com.fs.po.ClassTec;
import com.fs.utils.CmdUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

/**
 * 学生选课列表适配器
 * @author chenwenfei
 *
 */
public class StuSelectClassesAdapter extends BaseAdapter{
	
	private Context context;
	
	private List<ClassTec>  List;
	
	private int len=0;
	
	public StuSelectClassesAdapter(Context context) {

		this.context=context;
		
	}

	
	
	/**
	 * setData为适配器设置数据
	 */
	public void setData(List<ClassTec>  List) {
		
		this.List=List;
		
		this.len=List.size();
		
//		Toast.makeText(context, "Len"+len, 0).show();//【测试】
		
	}
	
	
	@Override
	public int getCount() {

		return len;
	}

	@Override
	public Object getItem(int position) {

		return position;
	}

	@Override
	public long getItemId(int position) {

		return position;
	}

	
	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		
		ViewHolder viewHolder=null;
		if(view==null){
			view = ((Activity) context).getLayoutInflater().inflate(R.layout.student_select_classes_tab, null);
			viewHolder = new ViewHolder();// 创建内部类对象，保存列表项中子控件的引用；
			
			viewHolder.tecNameText=(TextView) view.findViewById(R.id.student_select_classes_tecName);
			viewHolder.classText=(TextView) view.findViewById(R.id.student_select_classes_className);
			viewHolder.checkClass=(CheckBox) view.findViewById(R.id.student_select_classes_check);
			
			view.setTag(viewHolder);
			
		}else if (view != null) {
			
			   viewHolder=(ViewHolder) view.getTag();
			   
		  }
			viewHolder.tecNameText.setText(this.List.get(position).getTecName());
			viewHolder.classText.setText(this.List.get(position).getClassName());
			
			viewHolder.checkClass.setChecked(List.get(position).isChecked());
			
			viewHolder.checkClass.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					
					CmdUtils cmdUtils;
					if(isChecked){
						
						if(!(List.get(position).isChecked())){
							
							cmdUtils=new CmdUtils(context);
										
							String cmd="insert into data(stu_id,tec_id,class_id)value("+List.get(position).getStu_id()+","+List.get(position).getTec_id()+","+List.get(position).getClass_id()+")";
										
							 		
							cmdUtils.submitCmd(URL.CMD_URL, "add", cmd);
							
						}
						
					}else if(!isChecked){
						
						cmdUtils=new CmdUtils(context);
									
						String cmd="delete from data where stu_id="+List.get(position).getStu_id()+" and tec_id="+List.get(position).getTec_id()+" and class_id="+List.get(position).getClass_id()+"";
									
						cmdUtils.submitCmd(URL.CMD_URL, "delete", cmd);
						
					}
					
				}
			});
			return view;
		
	}
	
	class ViewHolder{
		
		TextView tecNameText;
		TextView classText;
		CheckBox checkClass;
		
	}


}
